package com.gdpu.project.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.gdpu.project.service.TeacherService;
import com.gdpu.project.vo.Teacher;
import com.opensymphony.xwork2.ActionSupport;

public class TeacherAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private String teacherId;
	private String password;
	private Integer role;
	private String name;
	private String sex;
	private String nation;
	private Date birthday;
	private String idCard;
	private String college;
	private String department;
	private Date workTime;
	private String position;
	private String education;
	private String graduateCollege;
	private String profession;
	private String speciality;
	private String phone;
	private String mobile;
	private String qq;
	private String email;
	private String fax;
	private TeacherService teacherService;

	private HttpServletRequest request;
	private HttpSession session;
	public String getTeacherId() {
		return teacherId;
	}
	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getRole() {
		return role;
	}
	public void setRole(Integer role) {
		this.role = role;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getNation() {
		return nation;
	}
	public void setNation(String nation) {
		this.nation = nation;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getIdCard() {
		return idCard;
	}
	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
	public String getCollege() {
		return college;
	}
	public void setCollege(String college) {
		this.college = college;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public Date getWorkTime() {
		return workTime;
	}
	public void setWorkTime(Date workTime) {
		this.workTime = workTime;
	}
	public String getPosition() {
		return position;
	}
	public void setPosition(String position) {
		this.position = position;
	}
	public String getEducation() {
		return education;
	}
	public void setEducation(String education) {
		this.education = education;
	}
	public String getGraduateCollege() {
		return graduateCollege;
	}
	public void setGraduateCollege(String graduateCollege) {
		this.graduateCollege = graduateCollege;
	}
	public String getProfession() {
		return profession;
	}
	public void setProfession(String profession) {
		this.profession = profession;
	}
	public String getSpeciality() {
		return speciality;
	}
	public void setSpeciality(String speciality) {
		this.speciality = speciality;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getFax() {
		return fax;
	}
	public void setFax(String fax) {
		this.fax = fax;
	}
	public TeacherService getTeacherService() {
		return teacherService;
	}
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}
	
	/**
	 * 管理员增加普通教职工Action,Role为0
	 * @return
	 */
	public String teacherAddByAdminAction(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		
		Teacher teacher = new Teacher();
		teacher.setTeacherId(getTeacherId());
		teacher.setName(getName());
		teacher.setPassword(getTeacherId());
		teacher.setRole(getRole());
		teacher.setSex(getSex());
		teacher.setNation(getNation());
		teacher.setBirthday(getBirthday());
		teacher.setIdCard(getIdCard());
		teacher.setCollege(getCollege());
		teacher.setDepartment(getDepartment());
		teacher.setWorkTime(getWorkTime());
		teacher.setPosition(getPosition());
		teacher.setEducation(getEducation());
		teacher.setGraduateCollege(getGraduateCollege());
		teacher.setProfession(getProfession());
		teacher.setSpeciality(getSpeciality());
		teacher.setPhone(getPhone());
		teacher.setMobile(getMobile());
		teacher.setQq(getQq());
		teacher.setEmail(getEmail());
		teacher.setFax(getFax());
		Boolean flag = teacherService.addTeacher(teacher);
		

		if(flag){
			List<Teacher> teacherlist = teacherService.findAllTeacher(0);
			request.setAttribute("teacherlist", teacherlist);
			request.setAttribute("message", "新增成功！");
			return "success";
		}else{
			request.setAttribute("message", "新增失败，请重试！");
			return "failed";
		}
	}

	/**
	 * 管理员修改普通教职工Action,Role为0
	 * @return
	 */
	public String teacherUpdateByAdminAction(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		String teacherId = request.getParameter("teacherId").toString();
		Teacher teacher = teacherService.findTeacherById(teacherId);
		teacher.setTeacherId(getTeacherId());
		teacher.setName(getName());
		teacher.setRole(getRole());	
		teacher.setSex(getSex());
		teacher.setNation(getNation());
		teacher.setBirthday(getBirthday());
		teacher.setIdCard(getIdCard());
		teacher.setCollege(getCollege());
		teacher.setDepartment(getDepartment());
		teacher.setWorkTime(getWorkTime());
		teacher.setPosition(getPosition());
		teacher.setEducation(getEducation());
		teacher.setGraduateCollege(getGraduateCollege());
		teacher.setProfession(getProfession());
		teacher.setSpeciality(getSpeciality());
		teacher.setPhone(getPhone());
		teacher.setMobile(getMobile());
		teacher.setQq(getQq());
		teacher.setEmail(getEmail());
		teacher.setFax(getFax());
		
		Boolean flag = teacherService.updateTeacher(teacher);
		if(flag){
			List<Teacher> teacherlist = teacherService.findAllTeacher(0);
			request.setAttribute("teacherlist", teacherlist);
			request.setAttribute("message", "修改成功！");
			return "success";
		}else{
			request.setAttribute("message", "修改失败，请重试！");
			return "failed";
		}
	}
	
	
	/**
	 * 管理员查看教师列表
	 * @return
	 */
	public String findAllTeacher(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		List<Teacher> teacherlist = teacherService.findAllTeacher(0);
			request.setAttribute("teacherlist", teacherlist);
			return "success";
	}
	
	/**
	 * 查看管理员列表
	 * @return
	 */
	public String findAllAdmin(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		List<Teacher> teacherlist = teacherService.findAllTeacher(1);
			request.setAttribute("teacherlist", teacherlist);
			return "success";
	}
	
	/**
	 * 管理员查看教师信息
	 * @return
	 */
	public String findTeacher(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		String teacherId = request.getParameter("teacherId").toString();
		Teacher teacher = teacherService.findTeacherById(teacherId);
			request.setAttribute("teacher", teacher);
			return "success";
	}
	
	
	/**
	 * 教师查看自己信息
	 * @return
	 */
	public String findTeacherByTeacher(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		String teacherId =session.getAttribute("teacherId").toString();
		Teacher teacher = teacherService.findTeacherById(teacherId);
		request.setAttribute("teacher", teacher);
			return "success";
	}
	
	/**
	 * 教师修改自己信息
	 * @return
	 */
	public String updateTeacherByTeacher(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		String teacherId =request.getAttribute("teacherId").toString();
		Teacher teacher = teacherService.findTeacherById(teacherId);
		/*teacher.setTeacherId(getTeacherId());*/
		teacher.setName(getName());
		teacher.setRole(getRole());	
		teacher.setSex(getSex());
		teacher.setNation(getNation());
		teacher.setBirthday(getBirthday());
		teacher.setIdCard(getIdCard());
		teacher.setCollege(getCollege());
		teacher.setDepartment(getDepartment());
		teacher.setWorkTime(getWorkTime());
		teacher.setPosition(getPosition());
		teacher.setEducation(getEducation());
		teacher.setGraduateCollege(getGraduateCollege());
		teacher.setProfession(getProfession());
		teacher.setSpeciality(getSpeciality());
		teacher.setPhone(getPhone());
		teacher.setMobile(getMobile());
		teacher.setQq(getQq());
		teacher.setEmail(getEmail());
		teacher.setFax(getFax());
		Boolean flag = teacherService.updateTeacher(teacher);
		Teacher teacher1 = teacherService.findTeacherById(teacherId);
		if(flag){
			request.setAttribute("message", "修改成功！");
			request.setAttribute("teacher", teacher1);
			return "success";
		}else{
			request.setAttribute("message", "修改失败，请重试！");
			return "failed";
		}
	}
	
	
	/**
	 * 管理员删除教师信息
	 * @return
	 */
	public String teacherDeleteByAdminAction(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		String teacherId = request.getParameter("teacherId").toString();
		Boolean flag = teacherService.deleteTeacher(teacherService.findTeacherById(teacherId));
		if(flag){
			List<Teacher> teacherlist = teacherService.findAllTeacher(0);
			request.setAttribute("teacherlist", teacherlist);
			request.setAttribute("message", "删除成功！");
			return "success";
		}else{
			request.setAttribute("message", "删除失败，请重试！");
			return "failed";
		}
	}
	
	/**
	 * 普通教职工修改个人信息
	 */
	public String teacherUpdateAction(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		
		String teacherId = session.getAttribute("teacherId").toString();
		Teacher teacher = teacherService.findTeacherById(teacherId);
		teacher.setTeacherId(getTeacherId());
		teacher.setName(getName());
		teacher.setSex(getSex());
		teacher.setNation(getNation());
		teacher.setBirthday(getBirthday());
		teacher.setIdCard(getIdCard());
		teacher.setCollege(getCollege());
		teacher.setDepartment(getDepartment());
		teacher.setWorkTime(getWorkTime());
		teacher.setPosition(getPosition());
		teacher.setEducation(getEducation());
		teacher.setGraduateCollege(getGraduateCollege());
		teacher.setProfession(getProfession());
		teacher.setSpeciality(getSpeciality());
		teacher.setPhone(getPhone());
		teacher.setMobile(getMobile());
		teacher.setQq(getQq());
		teacher.setEmail(getEmail());
		teacher.setFax(getFax());
		
		Boolean flag = teacherService.updateTeacher(teacher);
		if(flag){
			request.setAttribute("messgae","更新成功!");
			return "success";
		}else{
			request.setAttribute("message", "更新失败，请重试！");
			return "failed";
			
		}
	}
	
	/**
	 * 用户修改密码
	 * @return
	 */
	public String teacherChangPwdAction(){		
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		
		String password1 = request.getAttribute("password").toString();
		String teacherId1 = session.getAttribute("teacherId1").toString();
		Teacher teacher = teacherService.findTeacherById(teacherId1);
		if(teacher.getPassword().equals(password1)){
			teacher.setPassword(password1);
			if(teacherService.updateTeacher(teacher)){
				request.setAttribute("message","密码修改成功");
				return "success";
			}else{
				request.setAttribute("message","密码修改失败,请重试！");
				return "failed";
			}
		}else{
			request.setAttribute("message","原始密码输入不正确,请重试！");
			return "failed";
		}
	}
	
	
	

}
